/*
 * Copyright 2015 Hannes Dorfmann.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.baseproject.mvp.delegate;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.baseproject.mvp.MvpBasePresenter;
import com.baseproject.mvp.MvpPresenter;
import com.baseproject.mvp.MvpView;

/**
 * The concrete implementation of {@link}
 *
 * @param <V> The type of {@link MvpView}
 * @param <P> The type of {@link MvpPresenter}
 * @author Hannes Dorfmann
 * @see ActivityMvpDelegate
 * @since 1.1.0
 */
public class ActivityMvpDelegateImpl<V extends MvpView, P extends MvpBasePresenter<V>>
        implements ActivityMvpDelegate {

    public static boolean DEBUG = false;
    private static final String DEBUG_TAG = "ActivityMvpDelegateImpl";

    private MvpDelegateCallback<V, P> delegateCallback;
    protected Activity activity;

    /**
     * @param activity         The Activity
     * @param delegateCallback The callback
     */
    public ActivityMvpDelegateImpl(@NonNull Activity activity,
                                   @NonNull MvpDelegateCallback<V, P> delegateCallback) {

        if (activity == null) {
            throw new NullPointerException("Activity is null!");
        }

        if (delegateCallback == null) {
            throw new NullPointerException("MvpDelegateCallback is null!");
        }
        this.delegateCallback = delegateCallback;
        this.activity = activity;
    }

    /**
     * Determines whether or not a Presenter Instance should be kept
     *
     * @param keepPresenterInstance true, if the delegate has enabled keep
     */
    static boolean retainPresenterInstance(boolean keepPresenterInstance, Activity activity) {
        return keepPresenterInstance && (activity.isChangingConfigurations()
                || !activity.isFinishing());
    }

    @Override
    public void onCreate(Bundle bundle) {
        P presenter = delegateCallback.createPresenter();
        if (presenter == null) {
            throw new NullPointerException(
                    "Presenter returned from createPresenter() is null. Activity is " + activity);
        }
        delegateCallback.setPresenter(presenter);
        getPresenter().attachView(getMvpView());
        getPresenter().start();
    }


    private P getPresenter() {
        P presenter = delegateCallback.getPresenter();
        if (presenter == null) {
            throw new NullPointerException("Presenter returned from getPresenter() is null");
        }
        return presenter;
    }

    private V getMvpView() {
        V view = delegateCallback.getMvpView();
        if (view == null) {
            throw new NullPointerException("View returned from getMvpView() is null");
        }
        return view;
    }

    @Override
    public void onDestroy() {
        getPresenter().detachView(false);
        if (!getPresenter().subscriptions.isUnsubscribed()) {
            getPresenter().subscriptions.unsubscribe();
        }
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onRestart() {

    }

    @Override
    public void onContentChanged() {

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
    }
}
